package com.yupi.springbootinit.model.vo;

import com.yupi.springbootinit.model.entity.OperationLog;
import com.yupi.springbootinit.utils.OperationLogContentUtils;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * 操作日志视图对象
 */
@Data
public class OperationLogVO {
    
    /**
     * 主键
     */
    private Long id;
    
    /**
     * 操作用户ID
     */
    private Long userId;
    
    /**
     * 操作用户名
     */
    private String userName;
    
    /**
     * 操作类型
     */
    private String operation;
    
    /**
     * 操作模块
     */
    private String module;
    
    /**
     * 操作描述
     */
    private String description;
    
    /**
     * 执行状态
     */
    private String status;
    
    /**
     * 执行时间（毫秒）
     */
    private Long executionTime;
    
    /**
     * 错误信息
     */
    private String errorMessage;
    
    /**
     * 请求参数
     */
    private String requestParams;
    
    /**
     * 返回结果
     */
    private String responseResult;
    
    /**
     * 操作IP
     */
    private String ipAddress;
    
    /**
     * 操作时间
     */
    private Date createTime;
    
    /**
     * 原始JSON内容
     */
    private String rawContent;
    
    /**
     * 从OperationLog实体转换为VO
     */
    public static OperationLogVO fromEntity(OperationLog operationLog) {
        if (operationLog == null) {
            return null;
        }
        
        OperationLogVO vo = new OperationLogVO();
        BeanUtils.copyProperties(operationLog, vo);
        
        // 解析JSON内容
        String content = operationLog.getContent();
        vo.setRawContent(content);
        
        if (content != null && !content.trim().isEmpty()) {
            vo.setDescription(OperationLogContentUtils.getDescription(content));
            vo.setStatus(OperationLogContentUtils.getStatus(content));
            vo.setExecutionTime(OperationLogContentUtils.getExecutionTime(content));
            vo.setErrorMessage(OperationLogContentUtils.getErrorMessage(content));
            vo.setRequestParams(OperationLogContentUtils.getRequestParams(content));
            vo.setResponseResult(OperationLogContentUtils.getResponseResult(content));
        }
        
        return vo;
    }
}